﻿using System;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Input;
using System.Windows.Media;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace SBPweb.PROvibe.Client.Framework.Controls
{

    internal class ContextMenuCatcher
    {

        private static List<ContextMenuCatcher> catchers = new List<ContextMenuCatcher>();
        private static ContextMenuCatcher lastCatcher;

        private UIElement control;

        internal ContextMenuCatcher(UIElement control)
        {
            this.control = control;
            catchers.Add(this);
            HtmlPage.Document.AttachEvent("oncontextmenu", OnContextMenu);
            HtmlPage.Document.AttachEvent("onmousedown", OnMouseDown);
            HtmlPage.Document.AttachEvent("onmousewheel", OnMouseWheel);
        }

        internal UIElement RelativeControl
        {
            get
            {
                return control;
            }
            set
            {
                control = value;
            }
        }

        internal int ZIndex;

        internal event EventHandler<HtmlClickEventArgs> Click;

        internal event EventHandler<HtmlClickEventArgs> ContextExpand;

        internal event EventHandler ContextCollapse;

        private void OnMouseDown(object sender, HtmlEventArgs e)
        {
            if (e.MouseButton == MouseButtons.Left && Click != null)
            {
                Click(this, new HtmlClickEventArgs(control, e));
            }
        }

        private void OnMouseWheel(object sender, HtmlEventArgs e)
        {
            object c = e.EventObject.GetProperty("wheelDelta");
            if (c != null)
            {
                int delta = int.Parse(c.ToString());
                if (delta > 0)
                {
                    //++
                }
                else
                {
                    //--
                }
            }
        }

        private void OnContextMenu(object sender, HtmlEventArgs e)
        {
            e.PreventDefault();
            e.StopPropagation();
            List<UIElement> list = new List<UIElement>(VisualTreeHelper.FindElementsInHostCoordinates(new Point(e.OffsetX, e.OffsetY), control));
            if (list.Count == 0)
            {
                return;
            }
            List<ContextMenuCatcher> sortedCatchers = new List<ContextMenuCatcher>();
            foreach (ContextMenuCatcher cmc in catchers)
            {
                if (list.Contains(cmc.control))
                {
                    cmc.ZIndex = list.IndexOf(cmc.control);
                    sortedCatchers.Add(cmc);
                }
            }
            sortedCatchers.Sort(ZIndexComparison);

            ContextMenuCatcher catcher = sortedCatchers[0];
            if (lastCatcher != null && catcher != lastCatcher)
            {
                if (lastCatcher.ContextCollapse != null)
                {
                    lastCatcher.ContextCollapse(lastCatcher, EventArgs.Empty);
                }
            }
            if (catcher.ContextExpand != null)
            {
                catcher.ContextExpand(catcher, new HtmlClickEventArgs(catcher.control, e));
                lastCatcher = catcher;
            }
        }

        private int ZIndexComparison(ContextMenuCatcher a, ContextMenuCatcher b)
        {
            return a.ZIndex.CompareTo(b.ZIndex);
        }

    }

}